<template>
  <el-dialog
    :title="!dataForm.prescribeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="问诊id" prop="consultationId">
      <el-input v-model="dataForm.consultationId" placeholder="问诊id"></el-input>
    </el-form-item>
    <el-form-item label="病人id" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="病人id"></el-input>
    </el-form-item>
    <el-form-item label="开药医生id" prop="doctorId">
      <el-input v-model="dataForm.doctorId" placeholder="开药医生id"></el-input>
    </el-form-item>
    <el-form-item label="患者疾病" prop="disease">
      <el-input v-model="dataForm.disease" placeholder="患者疾病"></el-input>
    </el-form-item>
    <el-form-item label="患者过敏史" prop="allergiesHistory">
      <el-input v-model="dataForm.allergiesHistory" placeholder="患者过敏史"></el-input>
    </el-form-item>
    <el-form-item label="患者生育计划" prop="fertilityStatus">
      <el-input v-model="dataForm.fertilityStatus" placeholder="患者生育计划"></el-input>
    </el-form-item>
    <el-form-item label="患者肝脏功能" prop="liverFunction">
      <el-input v-model="dataForm.liverFunction" placeholder="患者肝脏功能"></el-input>
    </el-form-item>
    <el-form-item label="患者病情描述" prop="diseaseDesc">
      <el-input v-model="dataForm.diseaseDesc" placeholder="患者病情描述"></el-input>
    </el-form-item>
    <el-form-item label="处方凭证" prop="prescriptionImg">
      <el-input v-model="dataForm.prescriptionImg" placeholder="处方凭证"></el-input>
    </el-form-item>
    <el-form-item label="开药时间" prop="prescribeTime">
      <el-input v-model="dataForm.prescribeTime" placeholder="开药时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          prescribeId: 0,
          consultationId: '',
          patientId: '',
          doctorId: '',
          disease: '',
          allergiesHistory: '',
          fertilityStatus: '',
          liverFunction: '',
          diseaseDesc: '',
          prescriptionImg: '',
          prescribeTime: '',
          options: [{
            value: '0',
            label: '无'
          }, {
            value: '1',
            label: '有'
          }]
        },
        dataRule: {
          consultationId: [
            { required: true, message: '问诊id不能为空', trigger: 'blur' }
          ],
          patientId: [
            { required: true, message: '病人id不能为空', trigger: 'blur' }
          ],
          doctorId: [
            { required: true, message: '开药医生id不能为空', trigger: 'blur' }
          ],
          disease: [
            { required: true, message: '患者疾病不能为空', trigger: 'blur' }
          ],
          allergiesHistory: [
            { required: true, message: '患者过敏史(0无1有)不能为空', trigger: 'blur' }
          ],
          fertilityStatus: [
            { required: true, message: '患者生育计划不能为空', trigger: 'blur' }
          ],
          liverFunction: [
            { required: true, message: '患者肝脏功能(0正常1异常)不能为空', trigger: 'blur' }
          ],
          diseaseDesc: [
            { required: true, message: '患者病情描述不能为空', trigger: 'blur' }
          ],
          prescriptionImg: [
            { required: true, message: '处方凭证不能为空', trigger: 'blur' }
          ],
          prescribeTime: [
            { required: true, message: '开药时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.prescribeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.prescribeId) {
            this.$http({
              url: this.$http.adornUrl(`/hos/prescribe/info/${this.dataForm.prescribeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.consultationId = data.prescribe.consultationId
                this.dataForm.patientId = data.prescribe.patientId
                this.dataForm.doctorId = data.prescribe.doctorId
                this.dataForm.disease = data.prescribe.disease
                this.dataForm.allergiesHistory = data.prescribe.allergiesHistory
                this.dataForm.fertilityStatus = data.prescribe.fertilityStatus
                this.dataForm.liverFunction = data.prescribe.liverFunction
                this.dataForm.diseaseDesc = data.prescribe.diseaseDesc
                this.dataForm.prescriptionImg = data.prescribe.prescriptionImg
                this.dataForm.prescribeTime = data.prescribe.prescribeTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hos/prescribe/${!this.dataForm.prescribeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'prescribeId': this.dataForm.prescribeId || undefined,
                'consultationId': this.dataForm.consultationId,
                'patientId': this.dataForm.patientId,
                'doctorId': this.dataForm.doctorId,
                'disease': this.dataForm.disease,
                'allergiesHistory': this.dataForm.allergiesHistory,
                'fertilityStatus': this.dataForm.fertilityStatus,
                'liverFunction': this.dataForm.liverFunction,
                'diseaseDesc': this.dataForm.diseaseDesc,
                'prescriptionImg': this.dataForm.prescriptionImg,
                'prescribeTime': this.dataForm.prescribeTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
